<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item label="订单编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['orderCode', validatorRules.orderCode]" placeholder="请输入订单编号"></a-input>
        </a-form-item>
        <a-form-item label="订单名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['orderName',validatorRules.orderName]" placeholder="请输入订单名称"></a-input>
        </a-form-item>
        <a-form-item label="订单类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['orderType']" :trigger-change="true" dictCode="order_type" placeholder="请选择订单类型"/>
        </a-form-item>
        <a-form-item label="供应商编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-popup
            v-decorator="['supplierCode']"
            :trigger-change="true"
            org-fields="id,supplier_code,supplier_name"
            dest-fields="supplierId,supplierCode,supplierName"
            code="supplier_search"
            @callback="popupCallback"/>
        </a-form-item>
        <a-form-item label="供应商名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['supplierName']" placeholder="请输入供应商名称"></a-input>
        </a-form-item>
        <a-form-item label="成品料号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-popup v-decorator="['materielCode']" :trigger-change="true" org-fields="id,materiel_code,product_name,gauge,unit"
            dest-fields="materielId,materielCode,materielName,gauge,unit" code="product_search" @callback="popupCallback" />
        </a-form-item>
        <a-form-item label="成品名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['materielName']" placeholder="请输入物料名称"></a-input>
        </a-form-item>
        <a-form-item label="规格" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['gauge']" placeholder="请输入规格"></a-input>
        </a-form-item>
        <a-form-item label="单位" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['unit']" placeholder="请输入单位"></a-input>
        </a-form-item>
        <a-form-item label="总数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['grossAccount']" placeholder="请输入总数量"></a-input>
        </a-form-item>
        <a-form-item label="凭证日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择凭证日期" v-decorator="['verifyDate']" :trigger-change="true" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="公司编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-popup
            v-decorator="['companyCode']"
            :trigger-change="true"
            org-fields="id,company_code,company_nameone"
            dest-fields="companyId,companyCode,companyName"
            code="company_search"
            @callback="popupCallback"/>
        </a-form-item>
        <a-form-item label="公司名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['companyName']" placeholder="请输入公司名称"></a-input>
        </a-form-item>
        <a-form-item label="付款条款" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['payItem']" :trigger-change="true" dictCode="pay_item" placeholder="请选择付款条款"/>
        </a-form-item>
        <a-form-item label="总价" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['totalPrice']" placeholder="请输入总价"></a-input>
        </a-form-item>
        <a-form-item label="货币" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['currency']" :trigger-change="true" dictCode="current_money" placeholder="请选择货币"/>
        </a-form-item>
        <a-form-item label="汇率" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['exchangeRate']" placeholder="请输入汇率"></a-input>
        </a-form-item>
        <a-form-item label="国际贸易条件" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['tradeCondition']" :trigger-change="true" dictCode="international_tradetype" placeholder="请选择国际贸易条件"/>
        </a-form-item>
        <a-form-item label="街道/门牌号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['receiveAddress']" placeholder="请输入街道/门牌号"></a-input>
        </a-form-item>
        <a-form-item label="电话" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['phone']" placeholder="请输入电话"></a-input>
        </a-form-item>
        <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea v-decorator="['notes']" rows="4" placeholder="请输入备注"/>
        </a-form-item>
        <a-form-item v-show="false" label="成品料号ID" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-show="false" v-decorator="['materielId']" placeholder="请输入成品料号ID"></a-input>
        </a-form-item>
        <a-form-item v-show="false" label="供应商ID" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-show="false" v-decorator="['supplierId']" placeholder="请输入供应商ID"></a-input>
        </a-form-item>
        <a-form-item v-show="false" label="公司ID" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-show="false" v-decorator="['companyId']" placeholder="请输入公司ID"></a-input>
        </a-form-item>

      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"

  export default {
    name: "MesOrderPurchaseModal",
    components: {
      JDate,
      JDictSelectTag,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        validatorRules: {
          orderCode: {
            rules: [
              { required: true, message: '请输入订单编号!'},
            ]
          },
          orderName: {
            rules: [
              { required: true, message: '请输入订单名称!'},
            ]
          },
          supplierCode: {
            rules: [
              { required: true, message: '请输入供应商编号!'},
            ]
          },
          companyCode: {
            rules: [
              { required: true, message: '请输入公司编号!'},
            ]
          },
        },
        url: {
          add: "/order/mesOrderPurchase/add",
          edit: "/order/mesOrderPurchase/edit",
        }

      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'createBy','createTime','orderCode','orderName','orderType','supplierCode','supplierName','verifyDate','companyCode','companyName','payItem','totalPrice','currency','exchangeRate','tradeCondition','receiveAddress','phone','notes','supplierId','companyId', 'materielCode', 'materielName', 'gauge','unit', 'grossAccount', 'materielId'))
          this.form.setFieldsValue({orderType: '采购订单'})
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }

        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'createBy','createTime','orderCode','orderName','orderType','supplierCode','supplierName','verifyDate','companyCode','companyName','payItem','totalPrice','currency','exchangeRate','tradeCondition','receiveAddress','phone','notes','supplierId','companyId', 'materielCode', 'materielName', 'gauge','unit', 'grossAccount', 'materielId'))
      },


    }
  }
</script>
